import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import Flow from '@/layout/flow'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {title: 'Dashboard', icon: 'dashboard'}
    }]
  },
  {
    path: '/organizational', //组织架构
    component: Layout,
    name: 'organizational',
    redirect: '/organizational/index',
    meta: {title: 'Organizational'},
    children: [{
      path: 'index',
      name: 'organizationalIndex',
      component: () => import('@/views/organizational/index'),
      meta: {title: '组织架构', icon: 'dashboard'}
    },{
      path: 'member',
      name: 'memberlist',
      component: () => import('@/views/organizational/member'),
      meta: {title: '导入通讯录'}
    }]
  },
  {
    path: '/company',   // 企业设置
    component: Layout,
    name: 'company',
    redirect: '/company/info',
    meta: {title: 'company', sidebarOpen: true},
    children: [{
      path: 'info',
      name: 'companyinfo',
      component: () => import('@/views/companySet/index'),
      meta: {title: '基本信息', icon: 'form'}
    },
      {
        path: 'mailList',
        name: 'companyMailList',
        component: () => import('@/views/companySet/companyMailList'),
        meta: {title: '通讯录信息', icon: 'table'}
      },
      {
        path: 'replaceAdmin',
        name: 'companyReplaceAdmin',
        component: () => import('@/views/companySet/replaceAdmin'),
        meta: {title: '更换主管理员', icon: 'user'}
      },
      {
        path: 'setRole',
        name: 'companySetRole',
        component: () => import('@/views/companySet/setRole'),
        meta: {title: '设置管理角色', icon: 'tree'}
      },
      {
        path: 'setAdmin',
        name: 'companySetAdmin',
        component: () => import('@/views/companySet/setAdmin'),
        meta: {title: '设置子管理员', icon: 'nested'}
      }
    ]
  },
  {
    path: '/approvalFlow',  // 审批流程
    component: Flow,
    name: 'approvalFlow',
    redirect: '/approvalFlow/base',
    meta: {title: 'approvalFlow'},
    children: [{
      path: 'base',
      name: 'approvalbase',
      component: () => import('@/views/approvalFlow/base'),
      meta: {title: '审批设置'}
    },
    {
      path: 'table',
      name: 'approvaltable',
      component: () => import('@/views/approvalFlow/table'),
      meta: {title: '表单设计'}
    },
    {
      path: 'flow',
      name: 'approvalflow',
      component: () => import('@/views/approvalFlow/flow'),
      meta: {title: '流程设计'}
    }]
  },
  {
    path: '/approval',  // 审批
    component: Layout,
    name: 'approval',
    redirect: '/approval/list',
    meta: {title: 'approval', sidebarOpen: true},
    children: [{
      path: 'list',
      name: 'list',
      component: () => import('@/views/approval/index'),
      meta: {title: '审批列表', icon:'form'}
    },
    {
      path: 'admin',
      name: 'admin',
      component: () => import('@/views/approval/admin'),
      meta: {title: '审批管理', icon:'form'}
    }]
  },
  {
    path: '/register',  // 签到
    component: Layout,
    name: 'register',
    redirect: '/register/list',
    meta: {title: 'register', sidebarOpen: true},
    children: [{
      path: 'list',
      name: 'registerlist',
      component: () => import('@/views/register/index'),
      meta: {title: '签到列表', icon:'form'}
    }]
  },
  {
    path: '/file',  // 文档
    component: Layout,
    name: 'file',
    redirect: '/file/list',
    meta: {title: 'file', sidebarOpen: true},
    children: [{
      path: 'list',
      name: 'filelist',
      component: () => import('@/views/file/index'),
      meta: {title: '文档列表', icon:'table'}
    }]
  },
  {
    path: '/attendance',  // 考勤
    component: Layout,
    name: 'attendance',
    redirect: '/attendance/list',
    meta: {title: 'attendance', sidebarOpen: true},
    children: [{
      path: 'list',
      name: 'attendancelist',
      component: () => import('@/views/attendance/index'),
      meta: {title: '考勤列表', icon:'form'}
    },
    {
      path: 'admin',
      name: 'attendanceadmin',
      component: () => import('@/views/attendance/admin'),
      meta: {title: '考勤管理', icon:'form'}
    },
    {
      path: 'addwork',
      name: 'attendanceaddwork',
      component: () => import('@/views/attendance/addwork'),
      meta: {title: '加班规则', icon:'form'}
    },
    {
      path: 'patchcard',
      name: 'attendancepatchcard',
      component: () => import('@/views/attendance/patchcard'),
      meta: {title: '补卡规则', icon:'form'}
    },
    {
      path: 'vacation',
      name: 'attendancevacation',
      component: () => import('@/views/attendance/vacation'),
      meta: {title: '假期管理', icon:'form'}
    }]
  },
  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: {title: 'External Link', icon: 'link'}
      }
    ]
  },

  // 404 page must be placed at the end !!!
  {path: '*', redirect: '/404', hidden: true}
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({y: 0}),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
